package com.yqg.protection.controller;


import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.yqg.protection.common.Result;
import com.yqg.protection.common.interceptor.SysLog;
import com.yqg.protection.controller.dto.EquipmentTypeDto;
import com.yqg.protection.entity.EquipmentInputStore;
import com.yqg.protection.entity.EquipmentType;
import com.yqg.protection.entity.EquipmentUnit;
import com.yqg.protection.mapper.EquipmentTypeMapper;
import com.yqg.protection.service.IEquipmentInputStoreService;
import com.yqg.protection.service.IEquipmentTypeService;
import com.yqg.protection.service.IEquipmentUnitService;
import com.yqg.protection.utils.Constants;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;

/**
 * EquipmentTypeController 前端控制器
 *
 * @author zxf
 * @since 2024-07-31
 */
@RestController
@RequestMapping("/equipment-type")
public class EquipmentTypeController {

    @Resource
    private IEquipmentTypeService equipmentTypeService;

    @Resource
    private EquipmentTypeMapper equipmentTypeMapper;

    @Autowired
    private IEquipmentInputStoreService equipmentInputStoreService;

    @Resource
    private IEquipmentUnitService equipmentUnitService;

    /**
     * 新增或者更新
     */
    @PostMapping("/edit")
    @SysLog("新增或者修改设备类型")
    public Result save(@RequestBody EquipmentType equipmentType) {
        equipmentType.setIsDelete(0);
        return Result.success(equipmentTypeService.saveOrUpdate(equipmentType));
    }

    /**
     * 删除
     */
    @GetMapping("delete/{id}")
    @SysLog("删除设备类型")
    public Result delete(@PathVariable Integer id) {
        LambdaQueryWrapper<EquipmentInputStore> equipmentInputStoreQueryWrapper = new LambdaQueryWrapper<>();
        equipmentInputStoreQueryWrapper.eq(EquipmentInputStore::getType, id);
        equipmentInputStoreQueryWrapper.eq(EquipmentInputStore::getIsDelete, 0);
        List<EquipmentInputStore> list = equipmentInputStoreService.list(equipmentInputStoreQueryWrapper);
        LambdaQueryWrapper<EquipmentUnit> equipmentUnitQueryWrapper = new LambdaQueryWrapper<>();
        equipmentUnitQueryWrapper.eq(EquipmentUnit::getPartTypeId, id);
        equipmentUnitQueryWrapper.eq(EquipmentUnit::getIsDelete, 0);
        List<EquipmentUnit> list1 = equipmentUnitService.list(equipmentUnitQueryWrapper);
        if ((list != null && list.size() > 0) || (list1 != null && list1.size() > 0)) {
            return Result.error(Constants.CODE_500, "存在该类型设备，无法删除类型");
        }
        EquipmentType equipmentType = equipmentTypeService.getById(id);
        equipmentType.setIsDelete(1);
        return Result.success(equipmentTypeService.saveOrUpdate(equipmentType));
    }

    /**
     * 查询所有
     */
    @GetMapping("/all")
    public Result findAll(Integer systemId) {
        QueryWrapper<EquipmentType> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("system_id", systemId);
        queryWrapper.eq("is_delete", 0);
        List<EquipmentType> list = equipmentTypeService.list(queryWrapper);
        return Result.success(list);
    }

    /**
     * 根据id查询
     */
    @GetMapping("/{id}")
    public Result findOne(@PathVariable Integer id) {
        return Result.success(equipmentTypeService.getById(id));
    }

    /**
     * 分页查询
     */
    @GetMapping("/page")
    public Result findPage(@RequestParam Integer pageNum,
                           @RequestParam Integer pageSize,
                           String name) {
        QueryWrapper<EquipmentType> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("is_delete", 0);
        if (StringUtils.isNotEmpty(name)) {
            queryWrapper.like("name", name);
        }
        queryWrapper.orderByDesc("id");
        return Result.success(equipmentTypeService.page(new Page<>(pageNum, pageSize), queryWrapper));
    }

    @GetMapping("/pageTotal")
    @ApiOperation("分页查询设备类型数量")
    public Result pageTotal(@RequestParam Integer pageNum,
                           @RequestParam Integer pageSize,
                           Integer level,
                           Long organizationId,
                           String isAllClient,
                           @RequestParam List<Long> clientIds) {
        pageNum = pageNum * pageSize - pageSize;
        List<EquipmentTypeDto> equipmentTypeDtos = equipmentTypeMapper.pageTotal(pageNum, pageSize, level, organizationId, isAllClient, clientIds);
        Long count = equipmentTypeMapper.pageTotalCount(level, organizationId, isAllClient, clientIds);
        HashMap<String, Object> map = new HashMap<>();
        map.put("record", equipmentTypeDtos);
        map.put("total", count);
        return Result.success(map);
    }
}

